package com.das.mechanic_base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.das.mechanic_base.R;

public class X3MemberButtonView extends RelativeLayout {

    private Context mContext;

    /**
     * 背景未选中
     */
    private Drawable noSelectDraw;

    /**
     * 背景选中
     */
    private Drawable selectDraw;

    /**
     * 未选中的文字颜色
     */
    private int noSelectTextColor;

    /**
     * 选中的文字颜色
     */
    private int selectTextColor;

    private TextView tv_content;
    private ProgressBar pb_bar;
    private String textContent;
    private Drawable rightIcon;
    private ImageView iv_right;

    private boolean isClick;

    public X3MemberButtonView(Context context) {
        this(context,null);
    }

    public X3MemberButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        /**
         * 获取xml中设置的属性
         */
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.MemberButtonView);
        noSelectDraw = typedArray.getDrawable(R.styleable.MemberButtonView_no_select_bg);
        selectDraw = typedArray.getDrawable(R.styleable.MemberButtonView_select_bg);
        noSelectTextColor = typedArray.getColor(R.styleable.MemberButtonView_no_select_text_color,Color.WHITE);
        selectTextColor = typedArray.getColor(R.styleable.MemberButtonView_select_text_color,Color.WHITE);
        textContent = typedArray.getString(R.styleable.MemberButtonView_text_content);
        rightIcon = typedArray.getDrawable(R.styleable.MemberButtonView_right_icon);
        typedArray.recycle();

        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.x3_member_button_view, this);
        tv_content = findViewById(R.id.tv_content);
        pb_bar = findViewById(R.id.pb_bar);
        iv_right = findViewById(R.id.iv_right);

        tv_content.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
        tv_content.setText(textContent);

        if (rightIcon != null) {
            iv_right.setBackground(rightIcon);
        }

        /**
         * 初始状态为不可点击
         */
        changeBtnStatus(false);
    }

    public boolean getStatus(){
        return isClick;
    }

    /**
     * 改变按钮状态
     * @param isClick
     */
    public void changeBtnStatus(boolean isClick) {
        this.isClick = isClick;
        pb_bar.setVisibility(GONE);
        this.setBackground(isClick ? selectDraw : noSelectDraw);
        tv_content.setTextColor(isClick ? selectTextColor : noSelectTextColor);
        this.setOnClickListener(v -> {
            if (isClick) {
                pb_bar.setVisibility(VISIBLE);
                if (iOnClickAffirm != null) {
                    iOnClickAffirm.iOnClickAffirm();
                }
            }else {
                if (iOnNoClickAffirm != null) {
                    iOnNoClickAffirm.iOnNoClickAffirm();
                }
            }
        });
    }

    /**
     * 改变按钮状态
     * @param isClick
     */
    public void changeBtnStatusButClick(boolean isClick) {
        pb_bar.setVisibility(GONE);
        this.setBackground(isClick ? selectDraw : noSelectDraw);
        tv_content.setTextColor(isClick ? selectTextColor : noSelectTextColor);
        this.setOnClickListener(v -> {
            pb_bar.setVisibility(VISIBLE);
            if (iOnClickAffirm != null) {
                iOnClickAffirm.iOnClickAffirm();
            }
        });
    }

    public void showLoading() {
        pb_bar.setVisibility(VISIBLE);
    }

    public void reset() {
        pb_bar.setVisibility(GONE);
    }

    public void reset(int duration) {
        if (pb_bar == null) {
            return;
        }
        pb_bar.postDelayed(() -> {
            if (pb_bar != null) {
                pb_bar.setVisibility(GONE);
            }
        },duration);
    }

    public void changeText(String content) {
        tv_content.setText(content);
    }

    public String getTextContent() {
        return tv_content.getText().toString();
    }

    public void rightImgGVIs(boolean isSelect){
        if(isSelect){
            iv_right.setVisibility(VISIBLE);
        }else {
            iv_right.setVisibility(GONE);
        }
    }

    IOnClickAffirm iOnClickAffirm;

    public void setiOnClickAffirm(IOnClickAffirm iOnClickAffirm) {
        this.iOnClickAffirm = iOnClickAffirm;
    }

    public interface IOnClickAffirm {
        void iOnClickAffirm();

    }

    IOnNoClickAffirm iOnNoClickAffirm;

    public void setiOnNoClickAffirm(IOnNoClickAffirm iOnNoClickAffirm) {
        this.iOnNoClickAffirm = iOnNoClickAffirm;
    }

    public interface IOnNoClickAffirm {
        void iOnNoClickAffirm();
    }
}
